{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "a15ed1a0-ca70-41cb-9771-84dab19798ee",
   "metadata": {},
   "source": [
    "# 量化交易学习"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6e71fd79-ed0c-4b9b-877a-ea9b4d7b1a73",
   "metadata": {},
   "source": [
    "## 1 Tushaer和Talib简介"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "af99b476-b878-4f2e-b376-7637ec9e3f02",
   "metadata": {},
   "source": [
    "### 1.1 tushare简介\n",
    "* 免费、开源的python财经数据接口包\n",
    "* 获取股票、基金、期货、数字货币等行情数据\n",
    "* 返回的数据格式大部分都是pandas DataFrame类型\n",
    "* 底层调用的是各大网站的数据接口，如股票使用的是sina数据接口"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6de1f130-b710-46e4-b317-42bb00e67c38",
   "metadata": {},
   "source": [
    "**tushare pro**\n",
    "* tushare升级版，有丰富的接口数据\n",
    "* 与tushare有稍微的区别\n",
    "* 部分接口使用有积分限制\n",
    "* 注册链接https://tushare.pro/register?reg=650483"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ed614f1a-a97f-46d6-b476-dc9ef9bec958",
   "metadata": {},
   "source": [
    "### 1.2 talib简介\n",
    "* Python金融量化的高级库\n",
    "* 指标丰富\n",
    "* 免费开源\n",
    "* 轻松使用"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "06e48126-e766-4dfb-ba4c-fea34a191834",
   "metadata": {},
   "source": [
    "### 1.3 环境安装\n",
    "* tushare安装\n",
    "`pip install tushare`\n",
    "* talib安装\n",
    "  * https://www.lfd.uci.edu/~gohlke/pythonlibs/\n",
    "  * pip install 下载的对应安装whl文件\n",
    "  * 或者`pip install ta-lib --index=https://pypi.vnpy.com`\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b5298b15-cc52-49d9-a51a-1e891407310a",
   "metadata": {},
   "source": [
    "## 2 简单双均线买卖策略示例"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "df867c02-5c4d-4d0c-9b33-58510013cc92",
   "metadata": {},
   "outputs": [],
   "source": [
    "import tushare as ts\n",
    "import talib\n",
    "import pandas as pd\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "6ebe0549-c78c-4cd5-86b0-78e08b49918f",
   "metadata": {},
   "outputs": [],
   "source": [
    "def add_note(code):\n",
    "    '''\n",
    "    格式化股票代码00063->`00063,在股票代码前面添加符号`,防止忽略前面的0\n",
    "    param： code：股票代码\n",
    "    '''\n",
    "    return \"'\" + code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "id": "f51794ed-dae2-4945-8077-f9813bc3f680",
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_stock_data(code, start_time, end_time):\n",
    "    '''\n",
    "    获取数据并保存到csv文件\n",
    "    param： code：股票代码\n",
    "    return: filname: csv文件存储目录\n",
    "    '''\n",
    "    # 通过tushare获取数据，前复权后的数据\n",
    "    df_raw = ts.get_k_data(code, autype='qfg', start=start_time, end=end_time)\n",
    "    df_raw[\"code\"] = df_raw[\"code\"].apply(add_note)\n",
    "    # print(df_raw)\n",
    "    df_raw.to_csv(code + '.csv',index=False)\n",
    "    return code + '.csv'\n",
    "# help(ts.get_k_data)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "05235946-cb60-41f5-ba4d-475b8e8d0eb1",
   "metadata": {},
   "outputs": [],
   "source": [
    "def compare_diff(param1, parame2):\n",
    "    '''\n",
    "    比较两个均线数值，如果param1>parame2，就返回1,否则就返回-1\n",
    "    '''\n",
    "    return np.where(param1>=parame2,1,-1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "a56652cf-5bbb-49e4-9868-b5d7098a2beb",
   "metadata": {},
   "outputs": [],
   "source": [
    "def calculate_sma(filename, ma1=5, ma2=10):\n",
    "    '''\n",
    "    计算双均线\n",
    "    param: filename: 股票csv数据路径\n",
    "    param：ma1：短期均线天数\n",
    "    param：ma2: 长期均线天数\n",
    "    return：df: 包含双均线的DataFrame\n",
    "    '''\n",
    "    df = pd.read_csv(filename)\n",
    "    df['date'] = pd.to_datetime(df['date'])\n",
    "    df.set_index('date',inplace=True)\n",
    "    # Simple Moving Average SMA 简单移动均线\n",
    "    df['SMA_' + str(ma1)] = talib.MA(df['close'], timeperiod=ma1)\n",
    "    df['SMA_' + str(ma2)] = talib.MA(df['close'], timeperiod=ma2)\n",
    "    # df的数据窗口也可以求出简单移动均线\n",
    "    # df['SMA2_' + str(ma1)] = df['close'].rolling(5).mean() \n",
    "    df['compare'] = df.apply(lambda x: compare_diff(x['SMA_' + str(ma1)],x['SMA_' + str(ma2)]),axis=1)\n",
    "    df = df[9:]\n",
    "    df.to_csv('sma_data.csv')\n",
    "    return df\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "a829d272-aa02-4655-86b9-aae38b379a88",
   "metadata": {},
   "outputs": [],
   "source": [
    "def tag(match, compare):\n",
    "    '''\n",
    "    开平仓\n",
    "    param: match:DataFrame的match列\n",
    "    param: compare:DataFrame的compare列\n",
    "    '''\n",
    "    if match == False and compare == 1:\n",
    "        return '买入'\n",
    "    if match == False and compare == -1:\n",
    "        return '卖出'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "7bc26056-0c4f-4db9-93cf-f81e496bdbc2",
   "metadata": {},
   "outputs": [],
   "source": [
    "def sell_and_by(df, ma1=5, ma2=10):\n",
    "    '''\n",
    "    买入开仓，卖出平仓\n",
    "    '''\n",
    "    # 标记均线上穿和下穿\n",
    "    df['match'] = df['compare'] == df['compare'].shift(1)\n",
    "    # 开仓与平仓\n",
    "    df['tag'] = df.apply(lambda x : tag(x['match'],x['compare']),axis=1)\n",
    "    df.to_csv('result.csv')\n",
    "    return df\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "176ad734-abd1-4350-a013-706b17ddbf31",
   "metadata": {},
   "outputs": [],
   "source": [
    "def cal_profit():\n",
    "    '''\n",
    "    回测数据，计算盈亏\n",
    "    '''\n",
    "    print('===简单双均线策略===')\n",
    "    df = pd.read_csv('result.csv')\n",
    "    df['date'] = pd.to_datetime(df['date'])\n",
    "    df.set_index('date',inplace=True)\n",
    "\n",
    "    df['profit'] = 0\n",
    "    total = 0\n",
    "    win = 0\n",
    "    buyprice = 0\n",
    "    \n",
    "    for index, row in df.iterrows():\n",
    "        if row.tag=='买入':\n",
    "            buyprice = row.close\n",
    "        if row.tag == '卖出' and buyprice != 0:\n",
    "            total += 1\n",
    "            # 用这种方法赋值：\n",
    "            df.loc[index,'profit'] = row.close - buyprice\n",
    "            if row.close - buyprice > 0:\n",
    "                win += 1\n",
    "                \n",
    "    percent = round((win/total)*100,2)\n",
    "    prifit = round(df['profit'].sum(),2)\n",
    "    df.to_csv('prifit.csv')\n",
    "    print('开平仓次数：',total, '\\n盈利次数：',win, '\\n成功率：', percent, '\\n盈亏：', prifit)\n",
    "    return df\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "id": "6ffb9b40-601d-4095-86dc-6d89377c335c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "本接口即将停止更新，请尽快使用Pro版接口：https://tushare.pro/document/2\n",
      "===简单双均线策略===\n",
      "开平仓次数： 2 \n",
      "盈利次数： 2 \n",
      "成功率： 100.0 \n",
      "盈亏： 0.57\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\lhl\\AppData\\Local\\Temp\\ipykernel_23368\\2788725352.py:21: FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value '0.20999999999999996' has dtype incompatible with int64, please explicitly cast to a compatible dtype first.\n",
      "  df.loc[index,'profit'] = row.close - buyprice\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>open</th>\n",
       "      <th>close</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>volume</th>\n",
       "      <th>code</th>\n",
       "      <th>SMA_5</th>\n",
       "      <th>SMA_10</th>\n",
       "      <th>compare</th>\n",
       "      <th>match</th>\n",
       "      <th>tag</th>\n",
       "      <th>profit</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2024-10-21</th>\n",
       "      <td>2.28</td>\n",
       "      <td>2.28</td>\n",
       "      <td>2.30</td>\n",
       "      <td>2.25</td>\n",
       "      <td>535233.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.244</td>\n",
       "      <td>2.290</td>\n",
       "      <td>-1</td>\n",
       "      <td>False</td>\n",
       "      <td>卖出</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-10-22</th>\n",
       "      <td>2.27</td>\n",
       "      <td>2.31</td>\n",
       "      <td>2.31</td>\n",
       "      <td>2.26</td>\n",
       "      <td>588163.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.262</td>\n",
       "      <td>2.269</td>\n",
       "      <td>-1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-10-23</th>\n",
       "      <td>2.30</td>\n",
       "      <td>2.32</td>\n",
       "      <td>2.35</td>\n",
       "      <td>2.29</td>\n",
       "      <td>545657.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.278</td>\n",
       "      <td>2.273</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>买入</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-10-24</th>\n",
       "      <td>2.32</td>\n",
       "      <td>2.34</td>\n",
       "      <td>2.36</td>\n",
       "      <td>2.30</td>\n",
       "      <td>559792.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.304</td>\n",
       "      <td>2.272</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-10-25</th>\n",
       "      <td>2.34</td>\n",
       "      <td>2.40</td>\n",
       "      <td>2.41</td>\n",
       "      <td>2.33</td>\n",
       "      <td>698342.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.330</td>\n",
       "      <td>2.286</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-10-28</th>\n",
       "      <td>2.41</td>\n",
       "      <td>2.55</td>\n",
       "      <td>2.56</td>\n",
       "      <td>2.40</td>\n",
       "      <td>1223229.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.384</td>\n",
       "      <td>2.314</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-10-29</th>\n",
       "      <td>2.55</td>\n",
       "      <td>2.43</td>\n",
       "      <td>2.57</td>\n",
       "      <td>2.42</td>\n",
       "      <td>966858.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.408</td>\n",
       "      <td>2.335</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-10-30</th>\n",
       "      <td>2.43</td>\n",
       "      <td>2.49</td>\n",
       "      <td>2.57</td>\n",
       "      <td>2.41</td>\n",
       "      <td>919922.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.442</td>\n",
       "      <td>2.360</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-10-31</th>\n",
       "      <td>2.48</td>\n",
       "      <td>2.60</td>\n",
       "      <td>2.64</td>\n",
       "      <td>2.47</td>\n",
       "      <td>1276264.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.494</td>\n",
       "      <td>2.399</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-01</th>\n",
       "      <td>2.58</td>\n",
       "      <td>2.52</td>\n",
       "      <td>2.63</td>\n",
       "      <td>2.46</td>\n",
       "      <td>979676.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.518</td>\n",
       "      <td>2.424</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-04</th>\n",
       "      <td>2.50</td>\n",
       "      <td>2.51</td>\n",
       "      <td>2.51</td>\n",
       "      <td>2.46</td>\n",
       "      <td>496867.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.510</td>\n",
       "      <td>2.447</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-05</th>\n",
       "      <td>2.51</td>\n",
       "      <td>2.76</td>\n",
       "      <td>2.76</td>\n",
       "      <td>2.49</td>\n",
       "      <td>1675410.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.576</td>\n",
       "      <td>2.492</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-06</th>\n",
       "      <td>2.83</td>\n",
       "      <td>2.78</td>\n",
       "      <td>2.90</td>\n",
       "      <td>2.72</td>\n",
       "      <td>2077447.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.634</td>\n",
       "      <td>2.538</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-07</th>\n",
       "      <td>2.74</td>\n",
       "      <td>2.88</td>\n",
       "      <td>2.92</td>\n",
       "      <td>2.72</td>\n",
       "      <td>1590034.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.690</td>\n",
       "      <td>2.592</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-08</th>\n",
       "      <td>2.88</td>\n",
       "      <td>2.80</td>\n",
       "      <td>2.91</td>\n",
       "      <td>2.78</td>\n",
       "      <td>1148099.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.746</td>\n",
       "      <td>2.632</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-11</th>\n",
       "      <td>2.78</td>\n",
       "      <td>2.86</td>\n",
       "      <td>2.91</td>\n",
       "      <td>2.76</td>\n",
       "      <td>1224527.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.816</td>\n",
       "      <td>2.663</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-12</th>\n",
       "      <td>2.83</td>\n",
       "      <td>2.77</td>\n",
       "      <td>2.85</td>\n",
       "      <td>2.74</td>\n",
       "      <td>957518.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.818</td>\n",
       "      <td>2.697</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-13</th>\n",
       "      <td>2.73</td>\n",
       "      <td>2.71</td>\n",
       "      <td>2.76</td>\n",
       "      <td>2.66</td>\n",
       "      <td>796685.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.804</td>\n",
       "      <td>2.719</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-14</th>\n",
       "      <td>2.71</td>\n",
       "      <td>2.62</td>\n",
       "      <td>2.72</td>\n",
       "      <td>2.61</td>\n",
       "      <td>586380.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.752</td>\n",
       "      <td>2.721</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-15</th>\n",
       "      <td>2.63</td>\n",
       "      <td>2.53</td>\n",
       "      <td>2.66</td>\n",
       "      <td>2.52</td>\n",
       "      <td>675650.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.698</td>\n",
       "      <td>2.722</td>\n",
       "      <td>-1</td>\n",
       "      <td>False</td>\n",
       "      <td>卖出</td>\n",
       "      <td>0.21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-18</th>\n",
       "      <td>2.54</td>\n",
       "      <td>2.53</td>\n",
       "      <td>2.60</td>\n",
       "      <td>2.49</td>\n",
       "      <td>623219.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.632</td>\n",
       "      <td>2.724</td>\n",
       "      <td>-1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-19</th>\n",
       "      <td>2.54</td>\n",
       "      <td>2.55</td>\n",
       "      <td>2.56</td>\n",
       "      <td>2.46</td>\n",
       "      <td>544610.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.588</td>\n",
       "      <td>2.703</td>\n",
       "      <td>-1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-20</th>\n",
       "      <td>2.55</td>\n",
       "      <td>2.61</td>\n",
       "      <td>2.63</td>\n",
       "      <td>2.53</td>\n",
       "      <td>635216.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.568</td>\n",
       "      <td>2.686</td>\n",
       "      <td>-1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-21</th>\n",
       "      <td>2.60</td>\n",
       "      <td>2.59</td>\n",
       "      <td>2.60</td>\n",
       "      <td>2.56</td>\n",
       "      <td>367873.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.562</td>\n",
       "      <td>2.657</td>\n",
       "      <td>-1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-22</th>\n",
       "      <td>2.58</td>\n",
       "      <td>2.49</td>\n",
       "      <td>2.62</td>\n",
       "      <td>2.48</td>\n",
       "      <td>542787.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.554</td>\n",
       "      <td>2.626</td>\n",
       "      <td>-1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-25</th>\n",
       "      <td>2.49</td>\n",
       "      <td>2.45</td>\n",
       "      <td>2.51</td>\n",
       "      <td>2.41</td>\n",
       "      <td>532794.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.538</td>\n",
       "      <td>2.585</td>\n",
       "      <td>-1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-26</th>\n",
       "      <td>2.44</td>\n",
       "      <td>2.45</td>\n",
       "      <td>2.52</td>\n",
       "      <td>2.44</td>\n",
       "      <td>531981.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.518</td>\n",
       "      <td>2.553</td>\n",
       "      <td>-1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-27</th>\n",
       "      <td>2.45</td>\n",
       "      <td>2.47</td>\n",
       "      <td>2.47</td>\n",
       "      <td>2.36</td>\n",
       "      <td>607295.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.490</td>\n",
       "      <td>2.529</td>\n",
       "      <td>-1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-28</th>\n",
       "      <td>2.48</td>\n",
       "      <td>2.50</td>\n",
       "      <td>2.55</td>\n",
       "      <td>2.46</td>\n",
       "      <td>560133.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.472</td>\n",
       "      <td>2.517</td>\n",
       "      <td>-1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-11-29</th>\n",
       "      <td>2.50</td>\n",
       "      <td>2.53</td>\n",
       "      <td>2.54</td>\n",
       "      <td>2.46</td>\n",
       "      <td>442585.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.480</td>\n",
       "      <td>2.517</td>\n",
       "      <td>-1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-02</th>\n",
       "      <td>2.55</td>\n",
       "      <td>2.78</td>\n",
       "      <td>2.78</td>\n",
       "      <td>2.55</td>\n",
       "      <td>638503.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.546</td>\n",
       "      <td>2.542</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>买入</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-03</th>\n",
       "      <td>2.97</td>\n",
       "      <td>3.06</td>\n",
       "      <td>3.06</td>\n",
       "      <td>2.81</td>\n",
       "      <td>1769209.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.668</td>\n",
       "      <td>2.593</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-04</th>\n",
       "      <td>3.37</td>\n",
       "      <td>3.37</td>\n",
       "      <td>3.37</td>\n",
       "      <td>3.37</td>\n",
       "      <td>394606.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.848</td>\n",
       "      <td>2.669</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-05</th>\n",
       "      <td>3.71</td>\n",
       "      <td>3.71</td>\n",
       "      <td>3.71</td>\n",
       "      <td>3.71</td>\n",
       "      <td>363508.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>3.090</td>\n",
       "      <td>2.781</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-06</th>\n",
       "      <td>4.08</td>\n",
       "      <td>3.74</td>\n",
       "      <td>4.08</td>\n",
       "      <td>3.51</td>\n",
       "      <td>7780151.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>3.332</td>\n",
       "      <td>2.906</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-09</th>\n",
       "      <td>3.48</td>\n",
       "      <td>3.37</td>\n",
       "      <td>3.57</td>\n",
       "      <td>3.37</td>\n",
       "      <td>3438722.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>3.450</td>\n",
       "      <td>2.998</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-10</th>\n",
       "      <td>3.37</td>\n",
       "      <td>3.22</td>\n",
       "      <td>3.51</td>\n",
       "      <td>3.10</td>\n",
       "      <td>3634511.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>3.482</td>\n",
       "      <td>3.075</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-11</th>\n",
       "      <td>3.14</td>\n",
       "      <td>3.29</td>\n",
       "      <td>3.38</td>\n",
       "      <td>3.10</td>\n",
       "      <td>2622280.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>3.466</td>\n",
       "      <td>3.157</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-12</th>\n",
       "      <td>3.29</td>\n",
       "      <td>3.23</td>\n",
       "      <td>3.31</td>\n",
       "      <td>3.18</td>\n",
       "      <td>1907170.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>3.370</td>\n",
       "      <td>3.230</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-13</th>\n",
       "      <td>3.20</td>\n",
       "      <td>3.14</td>\n",
       "      <td>3.26</td>\n",
       "      <td>3.13</td>\n",
       "      <td>1498435.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>3.250</td>\n",
       "      <td>3.291</td>\n",
       "      <td>-1</td>\n",
       "      <td>False</td>\n",
       "      <td>卖出</td>\n",
       "      <td>0.36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-16</th>\n",
       "      <td>3.12</td>\n",
       "      <td>3.18</td>\n",
       "      <td>3.25</td>\n",
       "      <td>3.10</td>\n",
       "      <td>1510664.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>3.212</td>\n",
       "      <td>3.331</td>\n",
       "      <td>-1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-17</th>\n",
       "      <td>3.14</td>\n",
       "      <td>3.00</td>\n",
       "      <td>3.17</td>\n",
       "      <td>2.99</td>\n",
       "      <td>1497627.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>3.168</td>\n",
       "      <td>3.325</td>\n",
       "      <td>-1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-18</th>\n",
       "      <td>3.02</td>\n",
       "      <td>3.02</td>\n",
       "      <td>3.08</td>\n",
       "      <td>3.01</td>\n",
       "      <td>963648.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>3.114</td>\n",
       "      <td>3.290</td>\n",
       "      <td>-1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-19</th>\n",
       "      <td>2.99</td>\n",
       "      <td>2.99</td>\n",
       "      <td>3.03</td>\n",
       "      <td>2.95</td>\n",
       "      <td>930554.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>3.066</td>\n",
       "      <td>3.218</td>\n",
       "      <td>-1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-20</th>\n",
       "      <td>2.98</td>\n",
       "      <td>3.01</td>\n",
       "      <td>3.10</td>\n",
       "      <td>2.97</td>\n",
       "      <td>989737.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>3.040</td>\n",
       "      <td>3.145</td>\n",
       "      <td>-1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-23</th>\n",
       "      <td>3.02</td>\n",
       "      <td>2.86</td>\n",
       "      <td>3.03</td>\n",
       "      <td>2.85</td>\n",
       "      <td>1073292.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>2.976</td>\n",
       "      <td>3.094</td>\n",
       "      <td>-1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-24</th>\n",
       "      <td>2.89</td>\n",
       "      <td>3.15</td>\n",
       "      <td>3.15</td>\n",
       "      <td>2.89</td>\n",
       "      <td>2348689.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>3.006</td>\n",
       "      <td>3.087</td>\n",
       "      <td>-1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-25</th>\n",
       "      <td>3.15</td>\n",
       "      <td>3.20</td>\n",
       "      <td>3.32</td>\n",
       "      <td>3.10</td>\n",
       "      <td>3396008.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>3.042</td>\n",
       "      <td>3.078</td>\n",
       "      <td>-1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-26</th>\n",
       "      <td>3.14</td>\n",
       "      <td>3.09</td>\n",
       "      <td>3.14</td>\n",
       "      <td>3.06</td>\n",
       "      <td>1803347.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>3.062</td>\n",
       "      <td>3.064</td>\n",
       "      <td>-1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-27</th>\n",
       "      <td>3.05</td>\n",
       "      <td>3.10</td>\n",
       "      <td>3.17</td>\n",
       "      <td>3.02</td>\n",
       "      <td>1383342.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>3.080</td>\n",
       "      <td>3.060</td>\n",
       "      <td>1</td>\n",
       "      <td>False</td>\n",
       "      <td>买入</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-30</th>\n",
       "      <td>3.17</td>\n",
       "      <td>3.03</td>\n",
       "      <td>3.20</td>\n",
       "      <td>3.01</td>\n",
       "      <td>1249122.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>3.114</td>\n",
       "      <td>3.045</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2024-12-31</th>\n",
       "      <td>3.00</td>\n",
       "      <td>2.90</td>\n",
       "      <td>3.04</td>\n",
       "      <td>2.88</td>\n",
       "      <td>1179628.0</td>\n",
       "      <td>'000008</td>\n",
       "      <td>3.064</td>\n",
       "      <td>3.035</td>\n",
       "      <td>1</td>\n",
       "      <td>True</td>\n",
       "      <td>NaN</td>\n",
       "      <td>0.00</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            open  close  high   low     volume     code  SMA_5  SMA_10  \\\n",
       "date                                                                     \n",
       "2024-10-21  2.28   2.28  2.30  2.25   535233.0  '000008  2.244   2.290   \n",
       "2024-10-22  2.27   2.31  2.31  2.26   588163.0  '000008  2.262   2.269   \n",
       "2024-10-23  2.30   2.32  2.35  2.29   545657.0  '000008  2.278   2.273   \n",
       "2024-10-24  2.32   2.34  2.36  2.30   559792.0  '000008  2.304   2.272   \n",
       "2024-10-25  2.34   2.40  2.41  2.33   698342.0  '000008  2.330   2.286   \n",
       "2024-10-28  2.41   2.55  2.56  2.40  1223229.0  '000008  2.384   2.314   \n",
       "2024-10-29  2.55   2.43  2.57  2.42   966858.0  '000008  2.408   2.335   \n",
       "2024-10-30  2.43   2.49  2.57  2.41   919922.0  '000008  2.442   2.360   \n",
       "2024-10-31  2.48   2.60  2.64  2.47  1276264.0  '000008  2.494   2.399   \n",
       "2024-11-01  2.58   2.52  2.63  2.46   979676.0  '000008  2.518   2.424   \n",
       "2024-11-04  2.50   2.51  2.51  2.46   496867.0  '000008  2.510   2.447   \n",
       "2024-11-05  2.51   2.76  2.76  2.49  1675410.0  '000008  2.576   2.492   \n",
       "2024-11-06  2.83   2.78  2.90  2.72  2077447.0  '000008  2.634   2.538   \n",
       "2024-11-07  2.74   2.88  2.92  2.72  1590034.0  '000008  2.690   2.592   \n",
       "2024-11-08  2.88   2.80  2.91  2.78  1148099.0  '000008  2.746   2.632   \n",
       "2024-11-11  2.78   2.86  2.91  2.76  1224527.0  '000008  2.816   2.663   \n",
       "2024-11-12  2.83   2.77  2.85  2.74   957518.0  '000008  2.818   2.697   \n",
       "2024-11-13  2.73   2.71  2.76  2.66   796685.0  '000008  2.804   2.719   \n",
       "2024-11-14  2.71   2.62  2.72  2.61   586380.0  '000008  2.752   2.721   \n",
       "2024-11-15  2.63   2.53  2.66  2.52   675650.0  '000008  2.698   2.722   \n",
       "2024-11-18  2.54   2.53  2.60  2.49   623219.0  '000008  2.632   2.724   \n",
       "2024-11-19  2.54   2.55  2.56  2.46   544610.0  '000008  2.588   2.703   \n",
       "2024-11-20  2.55   2.61  2.63  2.53   635216.0  '000008  2.568   2.686   \n",
       "2024-11-21  2.60   2.59  2.60  2.56   367873.0  '000008  2.562   2.657   \n",
       "2024-11-22  2.58   2.49  2.62  2.48   542787.0  '000008  2.554   2.626   \n",
       "2024-11-25  2.49   2.45  2.51  2.41   532794.0  '000008  2.538   2.585   \n",
       "2024-11-26  2.44   2.45  2.52  2.44   531981.0  '000008  2.518   2.553   \n",
       "2024-11-27  2.45   2.47  2.47  2.36   607295.0  '000008  2.490   2.529   \n",
       "2024-11-28  2.48   2.50  2.55  2.46   560133.0  '000008  2.472   2.517   \n",
       "2024-11-29  2.50   2.53  2.54  2.46   442585.0  '000008  2.480   2.517   \n",
       "2024-12-02  2.55   2.78  2.78  2.55   638503.0  '000008  2.546   2.542   \n",
       "2024-12-03  2.97   3.06  3.06  2.81  1769209.0  '000008  2.668   2.593   \n",
       "2024-12-04  3.37   3.37  3.37  3.37   394606.0  '000008  2.848   2.669   \n",
       "2024-12-05  3.71   3.71  3.71  3.71   363508.0  '000008  3.090   2.781   \n",
       "2024-12-06  4.08   3.74  4.08  3.51  7780151.0  '000008  3.332   2.906   \n",
       "2024-12-09  3.48   3.37  3.57  3.37  3438722.0  '000008  3.450   2.998   \n",
       "2024-12-10  3.37   3.22  3.51  3.10  3634511.0  '000008  3.482   3.075   \n",
       "2024-12-11  3.14   3.29  3.38  3.10  2622280.0  '000008  3.466   3.157   \n",
       "2024-12-12  3.29   3.23  3.31  3.18  1907170.0  '000008  3.370   3.230   \n",
       "2024-12-13  3.20   3.14  3.26  3.13  1498435.0  '000008  3.250   3.291   \n",
       "2024-12-16  3.12   3.18  3.25  3.10  1510664.0  '000008  3.212   3.331   \n",
       "2024-12-17  3.14   3.00  3.17  2.99  1497627.0  '000008  3.168   3.325   \n",
       "2024-12-18  3.02   3.02  3.08  3.01   963648.0  '000008  3.114   3.290   \n",
       "2024-12-19  2.99   2.99  3.03  2.95   930554.0  '000008  3.066   3.218   \n",
       "2024-12-20  2.98   3.01  3.10  2.97   989737.0  '000008  3.040   3.145   \n",
       "2024-12-23  3.02   2.86  3.03  2.85  1073292.0  '000008  2.976   3.094   \n",
       "2024-12-24  2.89   3.15  3.15  2.89  2348689.0  '000008  3.006   3.087   \n",
       "2024-12-25  3.15   3.20  3.32  3.10  3396008.0  '000008  3.042   3.078   \n",
       "2024-12-26  3.14   3.09  3.14  3.06  1803347.0  '000008  3.062   3.064   \n",
       "2024-12-27  3.05   3.10  3.17  3.02  1383342.0  '000008  3.080   3.060   \n",
       "2024-12-30  3.17   3.03  3.20  3.01  1249122.0  '000008  3.114   3.045   \n",
       "2024-12-31  3.00   2.90  3.04  2.88  1179628.0  '000008  3.064   3.035   \n",
       "\n",
       "            compare  match  tag  profit  \n",
       "date                                     \n",
       "2024-10-21       -1  False   卖出    0.00  \n",
       "2024-10-22       -1   True  NaN    0.00  \n",
       "2024-10-23        1  False   买入    0.00  \n",
       "2024-10-24        1   True  NaN    0.00  \n",
       "2024-10-25        1   True  NaN    0.00  \n",
       "2024-10-28        1   True  NaN    0.00  \n",
       "2024-10-29        1   True  NaN    0.00  \n",
       "2024-10-30        1   True  NaN    0.00  \n",
       "2024-10-31        1   True  NaN    0.00  \n",
       "2024-11-01        1   True  NaN    0.00  \n",
       "2024-11-04        1   True  NaN    0.00  \n",
       "2024-11-05        1   True  NaN    0.00  \n",
       "2024-11-06        1   True  NaN    0.00  \n",
       "2024-11-07        1   True  NaN    0.00  \n",
       "2024-11-08        1   True  NaN    0.00  \n",
       "2024-11-11        1   True  NaN    0.00  \n",
       "2024-11-12        1   True  NaN    0.00  \n",
       "2024-11-13        1   True  NaN    0.00  \n",
       "2024-11-14        1   True  NaN    0.00  \n",
       "2024-11-15       -1  False   卖出    0.21  \n",
       "2024-11-18       -1   True  NaN    0.00  \n",
       "2024-11-19       -1   True  NaN    0.00  \n",
       "2024-11-20       -1   True  NaN    0.00  \n",
       "2024-11-21       -1   True  NaN    0.00  \n",
       "2024-11-22       -1   True  NaN    0.00  \n",
       "2024-11-25       -1   True  NaN    0.00  \n",
       "2024-11-26       -1   True  NaN    0.00  \n",
       "2024-11-27       -1   True  NaN    0.00  \n",
       "2024-11-28       -1   True  NaN    0.00  \n",
       "2024-11-29       -1   True  NaN    0.00  \n",
       "2024-12-02        1  False   买入    0.00  \n",
       "2024-12-03        1   True  NaN    0.00  \n",
       "2024-12-04        1   True  NaN    0.00  \n",
       "2024-12-05        1   True  NaN    0.00  \n",
       "2024-12-06        1   True  NaN    0.00  \n",
       "2024-12-09        1   True  NaN    0.00  \n",
       "2024-12-10        1   True  NaN    0.00  \n",
       "2024-12-11        1   True  NaN    0.00  \n",
       "2024-12-12        1   True  NaN    0.00  \n",
       "2024-12-13       -1  False   卖出    0.36  \n",
       "2024-12-16       -1   True  NaN    0.00  \n",
       "2024-12-17       -1   True  NaN    0.00  \n",
       "2024-12-18       -1   True  NaN    0.00  \n",
       "2024-12-19       -1   True  NaN    0.00  \n",
       "2024-12-20       -1   True  NaN    0.00  \n",
       "2024-12-23       -1   True  NaN    0.00  \n",
       "2024-12-24       -1   True  NaN    0.00  \n",
       "2024-12-25       -1   True  NaN    0.00  \n",
       "2024-12-26       -1   True  NaN    0.00  \n",
       "2024-12-27        1  False   买入    0.00  \n",
       "2024-12-30        1   True  NaN    0.00  \n",
       "2024-12-31        1   True  NaN    0.00  "
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if __name__ == '__main__':\n",
    "    code = '000008' # 股票代码\n",
    "    star_time = '2024-1-1'\n",
    "    end_time = '2024-12-31'\n",
    "    # 配置双均线\n",
    "    ma_5 = 5\n",
    "    ma_10 = 10\n",
    "    # 获取数据并保存到csv文件\n",
    "    filename = get_stock_data(code,star_time,end_time)\n",
    "    # 计算双均线数据并保存\n",
    "    df = calculate_sma(filename, ma_5, ma_10)\n",
    "    # 买入开仓，卖出平仓\n",
    "    sell_and_by(df, ma_5, ma_5)\n",
    "    # 计算盈利\n",
    "    df = cal_profit()\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "id": "92089402-0c19-4f2f-a68c-4d7fbc503101",
   "metadata": {},
   "outputs": [],
   "source": [
    "pro = ts.pro_api()\n",
    "\n",
    "#查询当前所有正常上市交易的股票列表\n",
    "\n",
    "data = pro.stock_basic(exchange='', list_status='L', fields='ts_code,symbol,name,area,industry,list_date')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "id": "f20a1a94-d37d-4117-be85-195580c8ed58",
   "metadata": {},
   "outputs": [],
   "source": [
    "data.to_csv('股票列表.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "id": "50be5117-1af0-439b-a487-238de52b491e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>change</th>\n",
       "      <th>pct_chg</th>\n",
       "      <th>vol</th>\n",
       "      <th>amount</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [ts_code, trade_date, open, high, low, close, pre_close, change, pct_chg, vol, amount]\n",
       "Index: []"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pro.daily(ts_code='920099.SZ', start_date='20241231', end_date='20250707')\n",
    "df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "id": "ed768f09-4faf-490c-9362-217f09f460c3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>ts_code</th>\n",
       "      <th>trade_date</th>\n",
       "      <th>open</th>\n",
       "      <th>high</th>\n",
       "      <th>low</th>\n",
       "      <th>close</th>\n",
       "      <th>pre_close</th>\n",
       "      <th>change</th>\n",
       "      <th>pct_chg</th>\n",
       "      <th>vol</th>\n",
       "      <th>amount</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Empty DataFrame\n",
       "Columns: [ts_code, trade_date, open, high, low, close, pre_close, change, pct_chg, vol, amount]\n",
       "Index: []"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
